<?php
#[文件管理]
$sysnav = "file";

if($sysAct == "upfiles")
{
	#[默认用新上传方法]
	$type = isset($type) ? $type : "new";
	require_once("class/unlimited_category.class.php");
	$CT = new Category();
	
	$rslist = $DB->qgGetAll("SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' ORDER BY taxis ASC");
	$catelist = $CT->arraySet($rslist,0);
	foreach($catelist AS $key=>$value)
	{
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
		$catelist[$key] = $value;
	}
	
	if($additonid)
	{
		$sql = "SELECT m.id,m.subject FROM ".$prefix."msg AS m,".$prefix."category AS c WHERE m.cateid=c.id AND m.additonid=0 AND c.sysgroupid='".$sysgroupid."' and m.cateid = '".$cateid."'";
		$sql .= " order by sortid asc,id desc";
		$msglist = $DB->qgGetAll($sql,true);
	}
}
elseif($sysAct == "upfilesok")
{
	#[允许上传的图片类型]
	$imgtype = array("jpg","jpeg","gif","png");
	#[读取系统组图片尺寸]
	if($sysgroupid)
	{
		$picsize = picsize($sysgroupid);
	}
	$filelist = $UP->name("iframeUpload");
	#[获取当前服务器信息]
	$mypath = $UP->getpath();
	foreach($filelist AS $key=>$value)
	{
		#[文件后缀名转小写]
		$tmarr = explode(".",$value);
		$tmpname = $tmarr[0].".".strtolower($tmarr[1]);
		$msgname = $tmarr[0];
		$filename = $UP->up("iframeUpload",$system_time."_".rander(),$key);
		if($filename)
		{
			$filetype = $UP->qgfiletype($filename);
			if(in_array($filetype,$imgtype))
			{
				#[生成缩略图]
				$thumbfile = $GD->thumb($mypath.$filename);
				#[生成水印图]
				$markfile = $GD->mark($mypath.$filename,$picsize["width"],$picsize["height"]);
			}
			
			#[上传的文件写入数据库中]
			$insert_id = $DB->qgInsert("INSERT INTO ".$prefix."upfiles(filetype,tmpname,filename,folder,postdate,thumbfile,markfile) VALUES('".$filetype."','".$tmpname."','".$filename."','".$mypath."','".$system_time."','".$thumbfile."','".$markfile."')");
			
			if($sysgroupid)
			{
				$sql = "INSERT INTO ".$prefix."msg(cateid,additonid,subject,subject_en,postdate,modifydate,thumb,orderdate) VALUES('".$cateid."','".$additonid."','".$msgname."','".$msgname."','".$system_time."','".$system_time."','".$insert_id."','".$system_time."')";
				$insert_id = $DB->qgInsert($sql);
			}
		}
	}
	$relink = $mainlink."?file=upfiles&act=upfiles&sysgroupid=".$sysgroupid."&cateid=".$cateid."&additonid=".$additonid."&type=standard";
	if($single)
	{
		$relink .= $relink."&single=".$single;
	}
	Error("附件上传成功！",$relink);
}
elseif($sysAct == "newupfilesok")
{
	#[允许上传的图片类型]
	$imgtype = array("jpg","jpeg","gif","png");
	#[读取系统组图片尺寸]
	if($sysgroupid)
	{
		$picsize = picsize($sysgroupid);
	}
	#[文件后缀名转小写]
	$filelist = $UP->name("file");
	$tmarr = explode(".",$filelist);
	$tmpname = $tmarr[0].".".strtolower($tmarr[1]);
	$msgname = $tmarr[0];
	#[上传图片]
	$filename = $UP->up("file",$system_time."_".rander());
	$folder = $mypath = $UP->getpath();
	if($filename)
	{
		$filetype = $UP->qgfiletype($filename);
		if(in_array($filetype,$imgtype))
		{
			#[生成缩略图]
			$thumbfile = $GD->thumb($mypath.$filename);
			#[生成水印图]
			$markfile = $GD->mark($mypath.$filename,$picsize["width"],$picsize["height"]);
		}
	}
	$updata["filetype"] = $filetype;
	$updata["tmpname"] = $tmpname;
	$updata["filename"] = $filename;
	$updata["folder"] = $folder;
	$updata["postdate"] = $system_time;
	$updata["thumbfile"] = $thumbfile;
	$updata["markfile"] = $markfile;
	$insertid = $DB->insert("upfiles",$updata);
	unset($updata);
	if($sysgroupid)
	{
		$updata["cateid"] = $cateid;
		if($additonid)
		{
			$updata["additonid"] = $additonid;
		}
		$updata["subject"] = $msgname;
		$updata["subject_en"] = $msgname;
		$updata["thumb"] = $insertid;
		$updata["postdate"] = $system_time;
		$updata["modifydate"] = $system_time;
		$updata["orderdate"] = $system_time;
		$DB->insert("msg",$updata);
		unset($updata);
	}
	
	$remess["cateid"] = $cateid;
	$remess["folder"] = $folder;
	$remess["filename"] = $filename;
	$remess["thumbfile"] = $thumbfile;
	$json = json_encode($remess);
	echo $json;
	exit;
}
elseif($sysact == "delete")
{
	$r_url = $_SESSION["return_url"] ? $_SESSION["return_url"] : $mainlink."?file=upfiles&act=list";
	if($idlist)
	{
		$myidlist = $STR->safe($idlist);#[获取IDlist]
		$idarray = explode(",",$myidlist);
		foreach($idarray AS $key=>$value)
		{
			$rsd = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id=".$value);
			if($rsd["filename"]){$FS->qgDelete($rsd["folder"].$rsd["filename"]);}
			if($rsd["thumbfile"]){$FS->qgDelete($rsd["folder"].$rsd["thumbfile"]);}
			if($rsd["markfile"]){$FS->qgDelete($rsd["folder"].$rsd["markfile"]);}
		}
		$DB->qgQuery("DELETE FROM ".$prefix."upfiles WHERE id in(".$idlist.")");
		Error("批量附件删除成功",$r_url);
	}
	else {
	$id = intval($id);
	chk_id($id,$r_url);
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$id."'");
	if(!$rs)
	{
		Error("附件不存在",$r_url);
	}
	if($rs["filename"])
	{
		$FS->qgDelete($rs["folder"].$rs["filename"]);
	}
	if($rs["thumbfile"])
	{
		$FS->qgDelete($rs["folder"].$rs["thumbfile"]);
	}
	if($rs["markfile"])
	{
		$FS->qgDelete($rs["folder"].$rs["markfile"]);
	}
	$DB->qgQuery("DELETE FROM ".$prefix."upfiles WHERE id='".$id."'");
	Error("附件删除成功",$r_url);
	}
}
elseif($sysact == "modifyok")
{
	$return_url = $_SESSION["return_url"] ? $_SESSION["return_url"] : $mainlink."?file=upfiles&act=list";
	$id = intval($id);
	chk_id($id,$return_url);
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$id."'");
	if(!$rs)
	{
		Error("附件不存在",$return_url);
	}
	$tmpname = $STR->safe($tmpname);
	if($tmpname == $rs["tmpname"])
	{
		Error("名称一样，不用换名字",$return_url);
	}
	$ext = strtolower(substr($rs["filename"],-3));
	if(strtolower(substr($tmpname,-3)) != $ext)
	{
		if($ext == ".gz")
		{
			$ext = "tar.gz";
		}
		$tmpname .= ".".$ext;
	}
	$DB->qgQuery("UPDATE ".$prefix."upfiles SET tmpname='".$tmpname."' WHERE id='".$id."'");
	Error("数据更新成功",$return_url);
}
elseif($sysAct == "list")
{
	#[显示session里popenValue信息]
	$page_url = $mainlink."?file=upfiles&act=list";
	$psize = 35;
	$pageid = intval($pageid);
	$offset = $pageid>0 ? ($pageid-1)*$psize : 0;
	#[显示图片列表]
	$condition = "WHERE 1";
	$keywords = SafeHtml($keywords);
	if($keywords)
	{
		$page_url .= "&keywords=".rawurlencode($keywords);
		$condition .= " AND tmpname LIKE '%".$keywords."%'";
	}
	$get_count = $DB->qgCount("SELECT id FROM ".$prefix."upfiles ".$condition);
	$sql = "SELECT * FROM ".$prefix."upfiles ".$condition." ORDER BY postdate DESC,id DESC LIMIT ".$offset.",".$psize;
	$rslist = $DB->qgGetAll($sql);
	$pagelist = page($page_url,$get_count,$psize,$pageid);#[获取页数信息]
	$filelist = array();
	foreach($rslist AS $key=>$value)
	{
		$value["postdate"] = date("Y-m-d H:i:s",$value["postdate"]);#[上传时间]
		if(strtolower(substr($value["folder"],0,7)) != "http://" && file_exists($value["folder"].$value["filename"]))
		{
			$filesize = filesize($value["folder"].$value["filename"]);
			if($value["thumbfile"] && file_exists($value["folder"].$value["thumbfile"]))
			{
				$filesize += filesize($value["folder"].$value["thumbfile"]);
				$value["thumb"] = $value["folder"].$value["thumbfile"];
			}
			if($value["markfile"] && file_exists($value["folder"].$value["markfile"]))
			{
				$filesize += filesize($value["folder"].$value["markfile"]);
				$value["mark"] = $value["folder"].$value["markfile"];
			}
			$value["filesize"] = $STR->num_format($filesize);
			#[检测附件是否是图片]
			if(strpos("jpg,gif,png",substr($value["filename"],-3)) !== false)
			{
				$value["thumb"] = $value["thumb"] ? $value["thumb"] : ($value["mark"] ? $value["mark"] : $value["folder"].$value["filename"]);
				$piclist[] = $value;
			}
			else
			{
				$attlist[] = $value;
			}
		}
		else
		{
			$extlist[] = $value;
		}
	}
	$_SESSION["return_url"] = $page_url."&pageid=".$pageid;
}
Foot("upfiles.qg");
?>